﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace kasy_4
{
    class Printer
    {
        private DataSet dataset;
        
        private Font printFont;
        private PrintTable printTable;

        public void PrintDocument(DataSet ds, PrintTable printTable)
        {
            try
            {
                dataset = ds;                
                //streamToPrint = new StreamReader("MyFile.txt");
                try
                {
                    this.printTable = printTable;
                    printFont = new Font("Courier", 12);
                    PrintDocument pd = new PrintDocument();                    
                    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                    pd.Print();
                }
                finally
                {
                    //streamToPrint.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Błąd drukowania", ex);
            }
        }
        
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

            Pen pen = new Pen(System.Drawing.Brushes.Black, 1);
            Brush brush = System.Drawing.Brushes.Black;
            Font fontTitle = new Font("Arial", 12, FontStyle.Regular);
            Font fontBigTitle = new Font("Arial", 12, FontStyle.Bold);
            Font fontHeader = new Font("Arial", 10, FontStyle.Regular);
            //---------------------------------------------------------------------------------------

            ev.Graphics.DrawString("KAN Sp. z o.o.", fontHeader, brush, leftMargin, 15, new StringFormat());
            ev.Graphics.DrawString("92-760 Łódź", fontHeader, brush, leftMargin, 30, new StringFormat());
            ev.Graphics.DrawString("ul. Wiączyńska 8a", fontHeader, brush, leftMargin, 45, new StringFormat());
            ev.Graphics.DrawLine(pen, 80, 70, 700, 70);
            ev.Graphics.DrawString(string.Format("Karton numer: {0}", this.printTable.BoxNumber.ToString()), fontBigTitle, brush, leftMargin, yPos + 80, new StringFormat());
            ev.Graphics.DrawString(string.Format("Wysyłka: {0}", this.printTable.DocumentName.ToString()), fontTitle, brush, leftMargin, yPos + 100, new StringFormat());
            ev.Graphics.DrawString(string.Format("Sklep :{0}", this.printTable.ShopName.ToString()), fontTitle, brush, leftMargin, yPos + 120, new StringFormat());
            ev.Graphics.DrawLine(pen, 80, 150, 700, 150);
            //----------------------------------------------------------------------------------------
            while (count < linesPerPage && count < dataset.Tables[0].Rows.Count)
            {
                object[] datasetTablesRowsItemArray = dataset.Tables[0].Rows[count].ItemArray;
                line = (count + 1).ToString().PadLeft(3) + " " + datasetTablesRowsItemArray[0].ToString().PadRight(20).PadLeft(3) + " " + datasetTablesRowsItemArray[1].ToString().PadLeft(6) + "\r\n";
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, leftMargin - 20, yPos + 60, new StringFormat());
                count++;
            }
            ev.Graphics.DrawLine(pen, 80, yPos + 80, 700, yPos + 80);
            ev.Graphics.DrawString(string.Format("Razem: {0}", this.printTable.AllQty), fontTitle, brush, leftMargin + 220, yPos + 90, new StringFormat());
            ev.Graphics.DrawLine(pen, 80, yPos + 120, 700, yPos + 120);
            ev.Graphics.DrawString(string.Format("Skompletowane przez\r\n{0}", this.printTable.Warehouseman), fontHeader, brush, leftMargin, yPos + 130, new StringFormat());
            ev.Graphics.DrawString(string.Format("Zatwierdzone przez\r\n{0}", this.printTable.Cashier), fontHeader, brush, leftMargin + 400, yPos + 130, new StringFormat());
            /*
            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
             */
        }

        /*
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
            
            Pen pen = new Pen(System.Drawing.Brushes.Black,1);
            Brush brush = System.Drawing.Brushes.Black;
            Font fontTitle = new Font("Arial",12,FontStyle.Regular);
            Font fontBigTitle = new Font("Arial", 12, FontStyle.Bold);
            Font fontHeader = new Font("Arial", 10, FontStyle.Regular);
            //---------------------------------------------------------------------------------------
            
            ev.Graphics.DrawString("KAN Sp. z o.o.", fontHeader, brush, leftMargin, 15, new StringFormat());
            ev.Graphics.DrawString("92-760 Łódź", fontHeader, brush, leftMargin, 30, new StringFormat());
            ev.Graphics.DrawString("ul. Wiączyńska 8a", fontHeader, brush, leftMargin, 45, new StringFormat());
            ev.Graphics.DrawLine(pen, 80, 70, 700, 70);
            ev.Graphics.DrawString(string.Format("Karton numer: {0}",this.printTable.BoxNumber.ToString()), fontBigTitle, brush, leftMargin, yPos+80, new StringFormat());
            ev.Graphics.DrawString(string.Format("Wysyłka: {0}" , this.printTable.DocumentName.ToString()), fontTitle, brush, leftMargin, yPos + 100, new StringFormat());
            ev.Graphics.DrawString(string.Format("Sklep :{0}",this.printTable.ShopName.ToString()), fontTitle, brush, leftMargin, yPos + 120, new StringFormat());            
            ev.Graphics.DrawLine(pen, 80, 150, 700, 150);
            //----------------------------------------------------------------------------------------
            while (count < linesPerPage && count<dataset.Tables[0].Rows.Count)
            {
                object[] datasetTablesRowsItemArray = dataset.Tables[0].Rows[count].ItemArray;
                line = (count + 1).ToString().PadLeft(3) + " " + datasetTablesRowsItemArray[0].ToString().PadRight(20).PadLeft(3) + " " + datasetTablesRowsItemArray[1].ToString().PadLeft(6) + "\r\n";
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, leftMargin-20, yPos+60, new StringFormat());
                count++;
            }
            ev.Graphics.DrawLine(pen, 80, yPos + 80, 700, yPos + 80);
            ev.Graphics.DrawString(string.Format("Razem: {0}",this.printTable.AllQty),fontTitle,brush,leftMargin+220,yPos+90,new StringFormat());
            ev.Graphics.DrawLine(pen, 80, yPos + 120, 700, yPos + 120);
            ev.Graphics.DrawString(string.Format("Skompletowane przez\r\n{0}", this.printTable.Warehouseman), fontHeader, brush, leftMargin, yPos + 130, new StringFormat());
            ev.Graphics.DrawString(string.Format("Zatwierdzone przez\r\n{0}", this.printTable.Cashier), fontHeader, brush, leftMargin+400, yPos + 130, new StringFormat());
            
            // If more lines exist, print another page.
            //if (line != null)
            //    ev.HasMorePages = true;
            //else
            //    ev.HasMorePages = false;
              
        }
    */
        private void pd_PrintPage_orig(object sender, PrintPageEventArgs ev)
        {
            /*
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
             */ 
        }

    }
}
